package digui;
 class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
public class Text1 {
    //题目: 力扣230题 二叉搜索树中第K小的元素
    int count;// 次数
    int ret = 0;  //结果
    public int kthSmallest(TreeNode root, int k) {
        //二叉搜索树的中序遍历是有序的
        count = k;
        dfs(root);
        return ret;
    }

    public void dfs(TreeNode root) {
        if(root == null || count == 0) {
            return;
        }
        //左子树
        dfs(root.left);
        count--;
        if(count == 0) {
            ret = root.val;
        }
        //剪枝
        if(count == 0) {
            return;
        }
        //右子树
        dfs(root.right);
    }

    public static void main(String[] args) {

    }
}
